package cc.comac.mom.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.EquipSparePartMasterDataService;
import cc.comac.mom.bean.EquipSparePartMasterData;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/EquipSparePartMasterData")
public class EquipSparePartMasterDataController {

	@Autowired
	private EquipSparePartMasterDataService equipSparePartMasterDataService;

	@RequestMapping(value = "/insertSaprePartMasterData", method = RequestMethod.POST)
	public ResultBean insert(@RequestBody EquipSparePartMasterData equipSparePartMasterData) {

		ResultBean resultBean = equipSparePartMasterDataService.insertSaprePartMasterData(equipSparePartMasterData);
		return resultBean;
	}

	@RequestMapping(value = "/updateSparePart", method = RequestMethod.POST)
	public ResultBean updateSparePart(@RequestBody EquipSparePartMasterData equipSparePartMasterData) {

		ResultBean resultBean = equipSparePartMasterDataService.updateSparePart(equipSparePartMasterData);

		return resultBean;
	}

	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public ResultBean batchDelete(@RequestBody List<EquipSparePartMasterData> list) {

		ResultBean resultBean = equipSparePartMasterDataService.batchDelete(list);
		return resultBean;
	}

	@RequestMapping(value = "/selectBysomeField", method = RequestMethod.POST)
	public ResultBean selectBysomeField(@RequestBody EquipSparePartMasterData equipSparePartMasterData) {
		ResultBean resultBean = new ResultBean(true);
		try {
			ResultDev selectBysomeField = equipSparePartMasterDataService.selectBysomeField(equipSparePartMasterData);
			resultBean.setObject(selectBysomeField);
		} catch (RuntimeException e) {
			resultBean.setErrorMessge(e.getMessage());
			return resultBean;
		}
		return resultBean;
	}

	@RequestMapping(value = "/selectBysparePartNumber", method = RequestMethod.POST)
	public ResultBean selectBysparePartNumber(@RequestBody EquipSparePartMasterData equipSparePartMasterData) {
		ResultBean resultBean = equipSparePartMasterDataService.selectBysparePartNumber(equipSparePartMasterData);
		return resultBean;
	}

	// 通过仓库信息查询仓库下所有入库的备件数据
	@RequestMapping(value = "/selectByWarehouseName", method = RequestMethod.POST)
	public ResultBean selectByWarehouseName(@RequestBody EquipSparePartMasterData equipSparePartMasterData) {
		ResultBean resultBean = new ResultBean(true);
		try {
			ResultDev selectByWarehouseName = equipSparePartMasterDataService
					.selectByWarehouseName(equipSparePartMasterData);
			resultBean.setObject(selectByWarehouseName);
		} catch (RuntimeException e) {
			resultBean.setErrorMessge(e.getMessage());
			return resultBean;
		}
		return resultBean;
	}

	// 通过仓库和备件编码查询备件数据
	@RequestMapping(value = "/selectByWarehouseNameAndSpareNumber", method = RequestMethod.POST)
	public ResultBean selectByWarehouseNameAndSpareNumber(
			@RequestBody EquipSparePartMasterData equipSparePartMasterData) {
		
			ResultBean resultBean = equipSparePartMasterDataService.selectByWarehouseNameAndSpareNumber(equipSparePartMasterData);
		return resultBean;
	}

	
	@ApiOperation(value = "备件主数据模板下载")
	@RequestMapping(value = "EquipSparePartMasterData/TemplateDownload")
	public @ResponseBody void TemplateDownload(HttpServletResponse request, HttpServletResponse response)
			throws Exception {
		String fileName = "备件主数据" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("sheet1");
		// HSSFCellStyle cellStyle = workbook.createCellStyle();
		//// 设置单元格水平方向对齐方式 居中
		// cellStyle.setAlignment(HorizontalAlignment.LEFT);
		HSSFCellStyle cellStyle2 = workbook.createCellStyle();
		// 设置字体样式
		HSSFFont font = workbook.createFont();
		font.setFontName("微软雅黑");
		// 设置字号
		font.setFontHeightInPoints((short) 12);
		cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.index);
		// 字体风格放入
		cellStyle2.setFont(font);

		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		ArrayList<String> listName = new ArrayList<String>();
		listName.add("备件编号(唯一性)");
		listName.add("备件类型编码");
		listName.add("备件名称");
		listName.add("规格型号");
		listName.add("品牌商");
		listName.add("单价");
		listName.add("各库存上限");
		listName.add("各库存下限");
		listName.add("库存数量");
		listName.add("计量单位");
		listName.add("关键参数");
		listName.add("备注");

		HSSFRow filedsRow = sheet.createRow(0);
		for (int i = 0; i < listName.size(); i++) {
			HSSFCell cell = filedsRow.createCell(i);
			cell.setCellStyle(cellStyle2);
			HSSFRichTextString text0 = new HSSFRichTextString(listName.get(i));
			cell.setCellValue(text0);
		}

		// 清空response
		response.reset();
		// 设置response的Header
		// 告诉浏览器用什么软件可以打开此文件
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition",
				"attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
		OutputStream out = response.getOutputStream();
		// response.setContentType("application/octet-stream;charset=utf-8");
		workbook.write(out);
		workbook.close();
		out.flush();
		out.close();
	}

	@ApiOperation(value = "备件主数据导入数据")
	@RequestMapping(value = "/EquipSparePartMasterData/TemplateDataUpload", method = RequestMethod.POST)
	public @ResponseBody ResultBean dataUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request)
			throws IOException {
		// 文件所在的临时文件夹
		String rootPath = request.getServletContext().getRealPath("/") + "uploadExcel";
		ResultBean resultBean = equipSparePartMasterDataService.importSparePartMasterData(file, rootPath);
		return resultBean;
	}

	@ApiOperation(value = "备件主数据导出数据")
	@RequestMapping("toExportExcel")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response,
			@RequestBody EquipSparePartMasterData obj) {
		String filePath = equipSparePartMasterDataService.exportData(request, response, obj);
		ExportExcel.download(filePath, response);
		ExportExcel.createDir(filePath);
	}

}
